package dictionary;



use vaaneeConfig qw(%C);
use Varg qw(%C);
use DepTree qw(%C);
use Align qw(%C);
use Features qw(%dbs %C);  

use AlignBeam qw(%C);
use Parameters qw(%C);
use LocalBeam qw(%C);

use PQ qw();

@ISA = qw(Exporter);
@EXPORT = qw();
@EXPORT_OK = qw(%dbs %C);


sub new {
	my($corpus) = {};
	my($classname) = shift;
	bless($corpus);
	
	my($srcFile) = shift;
	my($tgtFile) = shift;
	my($srcAlign) = shift;

	my($line);

	# Temporary variables
	my($a,$b,$c);
	my($sennum,$word,@words,@temparr,$tempsca);

	my($Varg,$Align,$AlignBeam,$Features,$Dep);

	my(@tempalns);
	my($i,$j,$k);
	my($srcword,$tgtword);
	my(%sentencenums);
	
	if($srcFile) {
		print STDERR "Loading Sens\n";
		$corpus->loadSen('SSen',$srcFile);
	}

	if($tgtFile) {
		print STDERR "Loading Tgt Sens\n";
		$corpus->loadSen('TSen',$tgtFile);
	}

	if($srcAlign) 
	{
		print STDERR "Loading Aligns\n";
		$corpus->loadAlign('Align',$srcAlign);
	}

	return $corpus;
}

sub loadSen {

	my($corpus) = shift;
	my($type) = shift;
	my($file) = shift;

	open(FILE,$file);

	my @lines = <FILE>;
	chomp(@lines);

	my $no_lines = @lines;
	my $senNum ;
	my $i=0;
	my $wordnum = 0;

	for($i=0; $i < $no_lines; $i++)
	{
		if($lines[$i] =~ /^\s*$/)
		{
		}
		elsif($lines[$i] =~ /^([0-9]+)$/)
		{
			$senNum = $1;
			$wordnum = 0;
		}
		else
		{
			my($word_POS_root,@features) = split(/\t/,$lines[$i]);

			my($word,$pos,$root) = split(/_/,$word_POS_root);
			$corpus->{$senNum}->{$type}->[$wordnum]->{'word'} = $word;
			$corpus->{$senNum}->{$type}->[$wordnum]->{'pos'} = $pos;
			$corpus->{$senNum}->{$type}->[$wordnum]->{'root'} = $root;

			# For 'prep' and 'aux' of the word. 

			my $feature;
			foreach $feature (@features)
			{
				my ($feat,$value) = split(/:/,$feature); 
				$corpus->{$senNum}->{$type}->[$wordnum]->{$feat} = $value;
			}
			$wordnum++;
		}
	}
}

sub loadAlign 
{
	my($corpus) = shift;
	my($type) = shift;
	my($file) = shift;

	open(FILE,$file) || die "Can't open $file\n";
	my @lines = <FILE>;

	my($line);
	my $no_lines = @lines;
	my $senNum;
	my $i=0;
	my $wordnum = 0;
	my $alignments;
	
	for($i=0; $i < $no_lines; $i++)
	{
		$line = $lines[$i]; 
		($senNum,$srcword,$tgtword) = split(/\s+/,$line);
#		print $srcword,"-->",$tgtword,"\n";
		$alignments->{$senNum}->{"$srcword $tgtword"} = 1;

	}

	foreach $senNum (keys %{$alignments}) {
		my $Align = new Align;

		foreach $line (keys %{$alignments->{$senNum}}) {
			($srcword,$tgtword) = split(/\s+/,$line);
#			print $srcword,"-->",$tgtword,"\n";
			$Align->addalign($srcword,$tgtword);
		}

		$corpus->{$senNum}->{$type} = $Align; 

	}
}


sub dictionary
{
	my($corpus) = shift;
	my($senNum) = shift;

#	rint "Here --> ",$corpus->{$senNum}->{'TSen'}->[0]->{'word'};
#	print "Here --> $senNum \n";
	my($Varg,$Dep);
	my($engsen,$hinsen);
	my($GoldAlign,$GenAlign,$GenAlign2);
	my($Beam,$TempBeam);
	my($LFeatures,$score);
	my($localbeam,%localbeams);
	my($k,$m,$n,$p,$i);
	my($minScore,$minPosition);
	my($hinwordpos);
	my($verbnum,$argnum);
	my($LFeatures,$GenFeatures);
	my($nengwords,$nhinwords);

#	print $Dep,"\n";
	$engsen = $corpus->{$senNum}->{'SSen'};
	$nengwords = @{$engsen};
	$hinsen = $corpus->{$senNum}->{'TSen'};
	$nhinwords = @{$hinsen};
	$Align = $corpus->{$senNum}->{'Align'};

#	print $engsen;
#	print $senNum,"\n";
	for($j = 0; $j < @{$corpus->{$senNum}->{'SSen'}}; $j++)
	{
		foreach $tgt (@{$Align->{'aln'}->[$j]})
		{
			print $engsen->[$j]->{'word'}," ---> ",$hinsen->[$tgt]->{'word'},"\n";
#			print $j," ---> ",$tgt,"\n";
		}
	}

}

1;
